﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// CircularProgressBar.xaml 的交互逻辑
    /// </summary>
    public partial class CircularProgressBar : UserControl
    {
        public CircularProgressBar()
        {
            InitializeComponent();
            SizeChanged += CircularProgressBar_SizeChanged;
        }

        private void CircularProgressBar_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.UpdateValue();
        }

        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(CircularProgressBar), new PropertyMetadata((double)0, new PropertyChangedCallback(OnPropertyChanged)));


        public double RingWidth
        {
            get { return (double)GetValue(RingWidthProperty); }
            set { SetValue(RingWidthProperty, value); }
        }

        public static readonly DependencyProperty RingWidthProperty =
            DependencyProperty.Register("RingWidth", typeof(double), typeof(CircularProgressBar), new PropertyMetadata((double)10.0, new PropertyChangedCallback(OnPropertyChanged)));


        public Brush ValueColor
        {
            get { return (Brush)GetValue(ValueColorProperty); }
            set { SetValue(ValueColorProperty, value); }
        }

        public static readonly DependencyProperty ValueColorProperty =
            DependencyProperty.Register("ValueColor", typeof(Brush), typeof(CircularProgressBar), new PropertyMetadata(Brushes.Black));


        public Brush BackColor
        {
            get { return (Brush)GetValue(BackColorProperty); }
            set { SetValue(BackColorProperty, value); }
        }

        public static readonly DependencyProperty BackColorProperty =
            DependencyProperty.Register("BackColor", typeof(Brush), typeof(CircularProgressBar), new PropertyMetadata(Brushes.LightGray, new PropertyChangedCallback(OnPropertyChanged)));


        public Brush ForeColor
        {
            get { return (Brush)GetValue(ForeColorProperty); }
            set { SetValue(ForeColorProperty, value); }
        }

        public static readonly DependencyProperty ForeColorProperty =
            DependencyProperty.Register("ForeColor", typeof(Brush), typeof(CircularProgressBar), new PropertyMetadata(Brushes.Orange, new PropertyChangedCallback(OnPropertyChanged)));

        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as CircularProgressBar).UpdateValue();
        }

        private void UpdateValue()
        {
            double len = Math.Min(ActualWidth, ActualHeight);
            double radius = len / 2;
            if (radius <= 0) return;

            if (RingWidth < 5)
                RingWidth = 6;
            double topGap = RingWidth * 0.5;
            this.path.StrokeThickness = RingWidth * 0.7;
            double R = radius - (RingWidth / 2);
            double newVal = Value % 100.0;
            double newX = radius + R * Math.Cos((newVal * 3.6 - 90) * Math.PI / 180);
            double newY = radius + R * Math.Sin((newVal * 3.6 - 90) * Math.PI / 180);
            //M75 3A75 75 0(phi) 0(小弧) 1(顺时针) 147 75
            string pathPattern = "M{0} {1}A{2} {2} 0  {3} 1 {4} {5}";
            pathPattern = string.Format(pathPattern, 
                radius,
                topGap,
                R,
                Value < 50 && Value > 0 ? 0 : 1,
                newX,
                newY);

            TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(Geometry));
            this.path.Data = (Geometry)typeConverter.ConvertFrom(pathPattern);
        }
    }
}
